package org.jetlinks.community.device.web;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.crud.service.ReactiveCrudService;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.jetlinks.community.device.entity.DemoEntity;
import org.jetlinks.community.device.response.DemoInfo;
import org.jetlinks.community.device.service.DemoService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;


@RestController
@RequestMapping("/demo")
@AllArgsConstructor
@Slf4j
public class DemoController implements ReactiveServiceCrudController<DemoEntity,String> {

    private DemoService demoService;

    @Override
    public ReactiveCrudService<DemoEntity, String> getService() {
        return demoService;
    }

    /**
     * 分页
     */
    @GetMapping("/list")
    public Mono<PagerResult<DemoInfo>> list(@RequestBody Mono<QueryParamEntity> query) {
        return query.flatMap(demoService::demoQueryPager);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Mono<Void> save(@RequestBody DemoEntity demo) {
        return demoService.demoSave(demo);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Mono<Integer> update(@RequestBody DemoEntity demo) {
        return demoService.demoUpdate(demo);
    }

    /**
     * 删除
     */
    @PutMapping("/remove")
    public Mono<Integer> remove(@RequestBody Mono<List<String>> idList) {
        return demoService.demoRemove(idList);
    }

    /**
     * 导出
     */
    @GetMapping("/export/{format}")
    public Mono<Void> export(ServerHttpResponse response,
                             @Parameter(hidden = true) QueryParamEntity parameter,
                             @PathVariable @Parameter(description = "文件格式,支持csv,xlsx") String format) throws IOException {
        response.getHeaders().set(HttpHeaders.CONTENT_DISPOSITION,
            "attachment; filename=".concat(URLEncoder.encode("demo." + format, StandardCharsets.UTF_8
                .displayName())));
        return demoService.export(response,parameter,format);
    }

//     导入mvc
//    @PostMapping("/demoImport")
//    public Mono<Void> doImport(@RequestParam("file") MultipartFile file) throws IOException {
//        return demoService.doImport(file);
//    }


    /**
     * 导入
     */
    @PostMapping("/demoImport")
    public Mono<Void> doImport(@RequestPart("file") Mono<FilePart> filePart) throws IOException {
        return  demoService.doImport(filePart);
    }
}
